//
//  E7GameChildLayer.cpp
//  e7engine
//
//  Created by cc on 12-3-14.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7GameChildLayer.h"

#include "E7Logic.h"

#include "E7GameLayer.h"

CE7GameChildLayer::CE7GameChildLayer()
{
    m_pCellSheet = NULL ;
    m_pCellRect = NULL ;

    m_pBackSheet = NULL ;

    m_pUnitSheet = NULL ;
    m_pUnitRect = NULL ;
}

CE7GameChildLayer::~CE7GameChildLayer()
{
//    SAFE_RELEASE(m_pCellSheet) ;
//    SAFE_DELETE_ARR(m_pCellRect) ;
//
//    SAFE_RELEASE(m_pBackSheet) ;
//
//    SAFE_RELEASE(m_pUnitSheet) ;
//    SAFE_DELETE_ARR(m_pUnitRect) ;

    removeAllChildrenWithCleanup(false) ;
}

void CE7GameChildLayer::InitBackSheet(const char* sheetfile, const unsigned int capacity, int z, int tag, CCNode* pParent)
{
    CCSpriteBatchNode* pBackSheet = new CCSpriteBatchNode() ;
    pBackSheet->initWithFile(sheetfile, capacity) ;

    if (NULL == pParent)
    {
        addChild(pBackSheet, z, tag) ;
    }
    else
    {
        pParent->addChild(pBackSheet) ;
    }
    
    CCSize backsize = pBackSheet->getTexture()->getContentSize() ;
    CCRect rcBackRect = CCRectMake(0, 0, backsize.width, backsize.height) ;
    
    printf("debug info: [%f] [%f]\n", backsize.width, backsize.height) ;
    if (IsPad())
    {
        int col = 3 ;
        int row = 3 ;
        int initx = backsize.width/2 ;
        int inity = 768 - backsize.height/2 ;
        for (int i = 0; i < capacity; ++i)
        {
            CCSprite* pBack = new CCSprite() ;
            pBack->initWithBatchNode(pBackSheet, rcBackRect) ;
            pBack->setPosition(ccp(initx + (int(i/col))*backsize.width, inity - (int(i%row))*backsize.height)) ;
            pBackSheet->addChild(pBack) ;
        }
    }
    else
    {
        int col = 3 ;
        int row = 3 ;
        int initx = backsize.width/2 ;
        int inity = 320 - backsize.height/2 ;
        for (int i = 0; i < capacity; ++i)
        {
            CCSprite* pBack = new CCSprite() ;
            pBack->initWithBatchNode(pBackSheet, rcBackRect) ;
            pBack->setPosition(ccp(initx + (int(i/col))*backsize.width, inity - (int(i%row))*backsize.height)) ;
            pBackSheet->addChild(pBack) ;
        }
    }
}

void CE7GameChildLayer::InitCellSheet(const char* sheetfile, const unsigned int capacity, int z, int tag)
{
    if (NULL != m_pCellSheet)
    {
        return ;
    }

    CCTexture2D *pTexture2D = CCTextureCache::sharedTextureCache()->addImage(sheetfile);
    pTexture2D->generateMipmap() ;
    ccTexParams texParm = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE } ;
    pTexture2D->setTexParameters(&texParm) ;
    m_pCellSheet = CCSpriteBatchNode::batchNodeWithTexture(pTexture2D, capacity) ;

    addChild(m_pCellSheet, z, tag) ;

    CCSize cellsize = m_pCellSheet->getTexture()->getContentSize() ;

    if (IsPad())
    {
        // width = 90, height = 78, count = 9.
        float width = MAP_CELL_WIDTH ;
        float height = MAP_CELL_HEIGHT ;
        int count = cellsize.width/width ;
        m_pCellRect = new CCRect[count] ;
        for (int i = 0; i < count; ++i)
        {
            m_pCellRect[i] = CCRectMake(i*width, 0, width, height) ;
        }
    }
    else
    {
        // width = 90, height = 78, count = 9.
        float width = MAP_CELL_WIDTH_IPHONE ;
        float height = MAP_CELL_HEIGHT_IPHONE ;
        int count = cellsize.width/width ;
        m_pCellRect = new CCRect[count] ;
        for (int i = 0; i < count; ++i)
        {
            m_pCellRect[i] = CCRectMake(i*width, 0, width, height) ;
        }
    }
}

void CE7GameChildLayer::InitUnitSheet(const char* sheetfile, const unsigned int capacity, int z, int tag)
{
    m_pUnitSheet = new CCSpriteBatchNode() ;
    m_pUnitSheet->initWithFile(sheetfile, capacity) ;
    addChild(m_pUnitSheet, z, tag) ;

    if (IsPad())
    {
        // width 720 / 9, height = 78, count = 8.
        float width = MAP_UNIT_WIDTH ;
        float height = MAP_UNIT_HEIGHT ;
        int count = 8 ;
        m_pUnitRect = new CCRect[count] ;
        for (int i = 0; i < count; ++i)
        {
            m_pUnitRect[i] = CCRectMake(i*width, 0, width, height) ;
        }
        m_pUnitRootLayer = new CCLayer() ;
        addChild(m_pUnitRootLayer, z, tag+1) ;
    }
    else
    {
        // width 720 / 9, height = 78, count = 8.
        float width = MAP_UNIT_WIDTH_IPHONE ;
        float height = MAP_UNIT_HEIGHT_IPHONE ;
        int count = 8 ;
        m_pUnitRect = new CCRect[count] ;
        for (int i = 0; i < count; ++i)
        {
            m_pUnitRect[i] = CCRectMake(i*width, 0, width, height) ;
        }
        m_pUnitRootLayer = new CCLayer() ;
        addChild(m_pUnitRootLayer, z, tag+1) ;
    }
}

void CE7GameChildLayer::InitArrowSheet(const char* sheetfile, const unsigned int capacity, int z, int tag)
{
    m_pArrowRootLayer = new CCLayer() ;
    addChild(m_pArrowRootLayer, z, tag) ;
}

void CE7GameChildLayer::DrawCell()
{
    try {
        m_pCellSheet->removeAllChildrenWithCleanup(false) ;
    } catch (...) {
        printf("CE7GameChildLayer::DrawCell() m_pCellSheet->removeAllChildrenWithCleanup(false) error\n") ;
    }

    CELL_INFO_LIST::iterator itr ;

//    srand((unsigned int)time(NULL)) ;

    if (IsPad())
    {
        CE7Cell* pInfo = NULL ;
        for (itr = s_pLogic->ActiveMap()->m_listCells.begin(); itr != s_pLogic->ActiveMap()->m_listCells.end(); ++itr)
        {
            pInfo = (*itr) ;
            if (-1 == pInfo->GetCountryID())
            {
                printf("cell index [%d]'s countryid is [%d]\n", pInfo->GetID(), pInfo->GetCountryID()) ;
                continue ;
            }

            CCSprite* pCell = CCSprite::spriteWithBatchNode(m_pCellSheet, m_pCellRect[pInfo->GetCountryID()]) ;
            pCell->setScaleX(MAP_SPRITE_SCALE_X) ;
            pCell->setScaleY(MAP_SPRITE_SCALE_Y) ;
            pCell->setPosition(pInfo->GetCenter()) ;
            m_pCellSheet->addChild(pCell) ;
        }
    }
    else
    {
        CE7Cell* pInfo = NULL ;
        for (itr = s_pLogic->ActiveMap()->m_listCells.begin(); itr != s_pLogic->ActiveMap()->m_listCells.end(); ++itr)
        {
            pInfo = (*itr) ;
            if (-1 == pInfo->GetCountryID())
            {
                printf("cell index [%d]'s countryid is [%d]\n", pInfo->GetID(), pInfo->GetCountryID()) ;
                continue ;
            }
            
            CCSprite* pCell = CCSprite::spriteWithBatchNode(m_pCellSheet, m_pCellRect[pInfo->GetCountryID()]) ;
            pCell->setScaleX(MAP_SPRITE_SCALE_X_IPHONE) ;
            pCell->setScaleY(MAP_SPRITE_SCALE_Y_IPHONE) ;
            pCell->setPosition(pInfo->GetCenter()) ;
            m_pCellSheet->addChild(pCell) ;
        }
    }
}

void CE7GameChildLayer::DrawUnit()
{
//    printf("enter draw unit function\n") ;
    m_pUnitSheet->removeAllChildrenWithCleanup(false) ;

    CELL_INFO_LIST::iterator itr ;
    CE7Cell* pInfo = NULL ;

    for (itr = s_pLogic->ActiveMap()->m_listCells.begin(); itr != s_pLogic->ActiveMap()->m_listCells.end(); ++itr)
    {
        pInfo = (*itr) ;
        if (-1 != pInfo->GetUnitID())
        {
            CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(pInfo->GetUnitID()) ;
            if (!pUnit->IsShow())
            {
                continue ;
            }

            if (UV_NONE < pUnit->GetUV() && UV_SOLDIER_MAX >= pUnit->GetUV())
            {
                eUnitValue uv = (eUnitValue)(pUnit->GetUV()-1) ;
                CCSprite* pChild = new CCSprite() ;
                pChild->initWithBatchNode(m_pUnitSheet, m_pUnitRect[uv]) ;
//                printf(" ===== draw unit : [%d] [%d]\n", uv, pUnit->GetUV()) ;
                switch (pUnit->GetUV())
                {
                    case UV_TREE:
                    {
                        pChild->setScale(0.8f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y+8)) ;
                    } break;
                    case UV_STONE:
                    {
                        pChild->setScale(0.6f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y)) ;
                    } break;
                    case UV_DEAD:
                    {
                        pChild->setScale(0.6f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y)) ;
                    } break;
                    case UV_SOLDIER_1:
                    {
                        pChild->setScale(0.6f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y)) ;
                    } break;
                    case UV_CITY:
                    {
                        pChild->setScale(0.6f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y+5)) ;
                    } break;
                    case UV_SOLDIER_2:
                    {
                        pChild->setScale(0.6f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y+3)) ;
                    } break;
                    case UV_TOWER:
                    {
                        pChild->setScale(0.8f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y+8)) ;
                    } break;
                    case UV_SOLDIER_3:
                    {
                        pChild->setScale(0.6f) ;
                        pChild->setPosition(ccp(pInfo->GetCenter().x, pInfo->GetCenter().y+8)) ;
                    } break;
                    default:
                        break;
                }
                
                m_pUnitSheet->addChild(pChild, pInfo->GetID(), pInfo->GetID()) ;
                if (s_pLogic->IsUserTurn() && 0 == pUnit->GetCountryID() && !pUnit->IsActioned())
                {
                    CE7AnimationSprite* pAnimationChild = new CE7AnimationSprite() ;
                    pAnimationChild->Init(pChild, ANI_SPRITE_TRANSFORM) ;
                    m_pUnitRootLayer->addChild(pAnimationChild) ;
                    pAnimationChild->Start() ;
                }
            }
        }
    }
}

static int flag = 0 ;
void CE7GameChildLayer::DrawAttackCell()
{
//    printf("enter DrawAttackCell \n") ;
//    if (0 != s_pLogic->GetTurnID())
//    {
//        return ;
//    }

    m_pArrowRootLayer->removeAllChildrenWithCleanup(false) ;
    if (NULL == s_pLogic->s_pSelect)
    {
        return ;
    }

    eUnitValue uv = UV_NONE ;
    if (UV_NONE == s_pLogic->s_eCreateUV)
    {
        if (UV_NONE == s_pLogic->s_eSelectUV)
        {
            return ;
        }
        uv = s_pLogic->s_eSelectUV ;
    }
    else if (UV_NONE == s_pLogic->s_eSelectUV)
    {
        if (UV_NONE == s_pLogic->s_eCreateUV)
        {
            return ;
        }

        if (UV_TOWER == s_pLogic->s_eCreateUV)
        {
            return ;
        }

        uv = s_pLogic->s_eCreateUV ;
    }
    else
    {
        if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV && UV_SOLDIER_1 == s_pLogic->s_eSelectUV)
        {
            uv = UV_SOLDIER_2 ;
        }
        else if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV && UV_SOLDIER_2 == s_pLogic->s_eSelectUV)
        {
            uv = UV_SOLDIER_3 ;
        }
        else if (UV_SOLDIER_2 == s_pLogic->s_eCreateUV && UV_SOLDIER_1 == s_pLogic->s_eSelectUV)
        {
            uv = UV_SOLDIER_3 ;
        }
        else
        {
            printf("error void CE7GameChildLayer::DrawAttackCell \n") ;
            return ;
        }
    }

    CE7Town* pTown = s_pLogic->GetSelectTownInfo(s_pLogic->s_pSelect->GetTownID()) ;
//    printf("now town [%d] cell count [%lu]\n", pTown->GetID(), pTown->m_listCellIDs.size()) ;
    for (CELL_ID_LIST::iterator itr = pTown->m_listCellIDs.begin(); itr != pTown->m_listCellIDs.end(); ++itr)
    {
        CE7Cell* pCell = s_pLogic->ActiveMap()->GetSelectCellInfo(*itr) ;
        for (int i = 0; i < 6; ++i)
        {
            CE7Cell* pNeighbor = pCell->GetNeighborPointer(i) ;
            if (NULL == pNeighbor)
            {
                continue ;
            }
            if (-1 == pNeighbor->GetCountryID())
            {
                continue ;
            }
            if (s_pLogic->s_pSelect->GetCountryID() == pNeighbor->GetCountryID())
            {
                continue ;
            }

            if (NULL != m_pArrowRootLayer->getChildByTag(pNeighbor->GetID()))
            {
                continue ;
            }

            pNeighbor->UpdateUV() ;
            if(pNeighbor->GetUV() >= uv)
            {
                continue ;
            }

            CE7AnimationArrow* pArrow = new CE7AnimationArrow() ;
            pArrow->Init(CCPointMake(pNeighbor->GetCenter().x, pNeighbor->GetCenter().y+20),
                         CCPointMake(pNeighbor->GetCenter().x, pNeighbor->GetCenter().y+10),
                         0.1f) ;
            pArrow->Start() ;
            m_pArrowRootLayer->addChild(pArrow) ;
        }
    }

    ++flag ;
//    printf("exit DrawAttackCell\n") ;
}

void CE7GameChildLayer::Render()
{
    DrawCell() ;
    DrawUnit() ;
    DrawAttackCell() ;
}

void CE7GameChildLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
}

void CE7GameChildLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
}

void CE7GameChildLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
}

void CE7GameChildLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
}














